<?php 

// Fetching data from the request sent by form.php    

$posttowall = "";
if ( isset($_POST["submission"]) ) { $posttowall = strip_tags($_POST["submission"]);
	}

$resetbutton = "";
if ( isset($_POST["resetbutton"]) ) { $resetbutton = strip_tags($_POST["resetbutton"]);
	}
	
$username = "";
if ( isset($_POST["username"]) ) { $username = strip_tags($_POST["username"]);
	}

$email = "";
if ( isset($_POST["email"]) ) { $email = strip_tags($_POST["email"]);
	}

$emailconfirm = "";
if ( isset($_POST["emailconfirm"]) ) { $emailconfirm = strip_tags($_POST["emailconfirm"]);
	}  

$comment = "";
if ( isset($_POST["comment"]) ) { $comment = strip_tags($_POST["comment"]); 
	}

$recommend = "";
if ( isset($_POST["recommend"]) ) { $recommend = strip_tags($_POST["recommend"]);
	}

  // If any fields are missing, record that there was an error.
  
  if ( $posttowall == ""){
	$error = True;
	require_once 'wall_database.php';

	$is_valid_post = true;
	//Fetching all posts from the database
	$posts_array = getAllPosts();
	}
  else{
	$error = False;
	}
  
  if ( $username == "" ) {
    $_POST["username_error"] = "Please enter a Name";
    $error = TRUE;
  } elseif (!ctype_alnum($username)) {
	$_POST["username_error"] = "Your name can only contain letters and numbers";
    $error = TRUE;
	} elseif(strlen($username)<=3) {
	$_POST["username_error"] = "Your name is too short";
    $error = TRUE;
	}
  // Two different possible errors so two different tests.
  if ( $email == "" ) {
    $_POST["email_error"] = "Please enter an email address";
    $error = TRUE;
  // (Checking that the emails match)
  } else if ( $emailconfirm != $email  ) {
    $_POST["email_error"] = "Your email addresses are not the same!";
    $error = TRUE;
  }
//comment
  if ( $comment == "" ) {  
	$_POST["comment_error"] = "Please enter a comment";
    $error = TRUE;
	} else if(strlen( $comment)<=3){
	$_POST["comment_error"] = "Your comment is too short";
    $error = TRUE;	
	} else if(strlen( $comment)>500){
	$_POST["comment_error"] = "Your comment is too long!";
    $error = TRUE;	
	}

	if ($posttowall == "Post to Wall"){
		if ($error == True){
			include("form.php");
			die();
		}  
		else{
		require_once 'wall_database.php';

		$is_valid_post = true;
		$post_fields = array();
		$post_fields['submission']=$posttowall;

		$post_fields['username']=$username;

		$post_fields['email']=$email;

		$post_fields['emailconfirm']=$emailconfirm;

		$post_fields['comment']=$comment;	

		$post_fields['recommend']=$recommend;	
		
		$post_fields['date']=date('M j, Y');	
		
		$success_flag = saveCurrentPost($post_fields);	
		//Fetching all posts from the database
		$posts_array = getAllPosts();
/*		echo "Thanks ".$username." for submitting your Comment, Question, etc.!</p>";
*/
		}
	}
require 'header.php';  
?>

<div id="wallcomment">

<p>
    <?php
    // Looping through all the posts in posts_array
	if (is_null("posts.dat") != TRUE){
    $counter = 1;
    foreach(array_reverse($posts_array) as $post){
      $username = $post['username'];
	  $comment = $post['comment'];
	  $recommend = $post['recommend'];
	  $date = $post['date'];
		  if ($counter % 2==1){
			$li_class = "float-left";
			}
		  else{
			$li_class = "float-right";
			}
	  echo "<div class='$li_class'>Date: ".$date."<br>".$username." Says:<br>".$comment."<br>";
	  echo "Recommend The Hot Truck to a Friend?: ".$recommend."<br></div>";
	  $counter = $counter + 1;
	  }
	}
	else{
	$li_class = "float-left";
	echo "<div class='$li_class'>How was the Hot Truck?</div>";
	}
    ?>
</p>
</div>
<?php
require 'footer.php';
?>